package com.example.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class RedisUtils {
    @Autowired
    private RedisTemplate redisTemplate;

    public RedisTemplate getRedisTemplate() {

        return this.redisTemplate;
    }

    public int incr(String key) {
        if (key == null) {
            key = "number";
        }
        Integer num = (Integer) redisTemplate.opsForValue().get(RedisKeys.incrOrdecr + key);
        if (num == null) {
            num = 1;
        } else {
            num++;
        }
        redisTemplate.opsForValue().set(RedisKeys.incrOrdecr + key, num);
        return num;
    }

    public int decr(String key) {
        if (key == null) {
            key = "number";
        }
        Integer num = (Integer) redisTemplate.opsForValue().get(RedisKeys.incrOrdecr + key);
        if (num == null || num == 0) {
            num = 0;
        } else {
            num--;
        }
        redisTemplate.opsForValue().set(RedisKeys.incrOrdecr + key, num);
        return num;
    }


}
